package com.software.chemistry.service.Impl;

import com.software.chemistry.entity.Message;
import com.software.chemistry.entity.StudentInfo;
import com.software.chemistry.entity.TeacherInfo;
import com.software.chemistry.entity.MsgList;
import com.software.chemistry.repository.MessageRepostiory;
import com.software.chemistry.repository.StudentInfoRepository;
import com.software.chemistry.repository.TeacherInfoRepositiory;
import com.software.chemistry.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
//实现service的相关接口同时完成相关业务的逻辑处理
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageRepostiory messageRepostiory ;
    @Autowired
    private StudentInfoRepository studentInfoRepository ;
    @Autowired
    private TeacherInfoRepositiory teacherInfoRepositiory ;

    @Override
    public boolean deleteMessageByReceiverId(String receiverId) {
        boolean flag = messageRepostiory.deleteMessagesByReceiverId(receiverId) ;
        return flag ;
    }

    @Override
    public boolean deleteMessageByReceiverIdAndType(String ReceiverId, long Type) {
        boolean flag = messageRepostiory.deleteMessagesByReceiverIdAndType(ReceiverId, Type) ;
        return flag ;
    }

    @Override
    public Message createMessage(Message message) {
//        message.setMessageId(UUID.randomUUID().toString());
//        message.setType(0);
        //  message.setMessageId("M"+UUID.randomUUID().toString());
        Message save = messageRepostiory.save(message) ;
        return save ;
    }

    @Override
    public List<MsgList> findMessagesByReceiverIdAndType(String receiverId, long type) {
        List<Message> messageList = messageRepostiory.findMessagesByReceiverIdAndTypeOrderByDate(receiverId, type) ;
        System.out.println();
        messageList.sort(new Comparator<Message>() {
            @Override
            public int compare(Message message, Message t1) {
                return t1.getDate().compareTo(message.getDate());
            }
        });
        //去重并记录出现多少次 id和次数
        Map<String,Integer> mapnum = new HashMap<>();
        Map<String, String> mapcontent = new HashMap<>();
        Map<String, String> maptime = new HashMap<>();
        //判断array中的元素是否在map当中，在就是1，不在就是在原来基础上加1
        for (int i = 0; i < messageList.size(); i++) {
            String id = messageList.get(i).getSenderId() ;
            System.out.println("id"+id);
            System.out.println("messageList: " + messageList.get(i).getDate());
            if(mapnum.get(id) == null){
                mapnum.put(id, 1);
                maptime.put(id, messageList.get(i).getDate());
                mapcontent.put(id,messageList.get(i).getMessageDetail());
            }else{
                mapnum.put(id,mapnum.get(id)+1);
            }
        }
        List<MsgList> msgLists = new ArrayList<>() ;
        //遍历map
        //由id查询头像icon 赋值给数组
        mapnum.forEach((key,value)->{
            //key是id
            //value是num
            MsgList msgList = new MsgList() ;
            msgList.setNum(value);
            msgList.setSendId(key) ;
            msgList.setTime(maptime.get(key));
            msgList.setMsgcontext(mapcontent.get(key));
            System.out.println("value" + value);
            System.out.println("key"+key);
            System.out.println("content: " + mapcontent.get(key)+ "   " + msgList.getMsgcontext());
            Optional<StudentInfo> optional = studentInfoRepository.findById(key) ;
            if (optional.isPresent()){
                StudentInfo studentInfo = optional.get() ;
                System.out.println(studentInfo);
                msgList.setIcon(studentInfo.getIcon());
                msgList.setName(studentInfo.getUserName());
            }
            else{
                Optional<TeacherInfo> optional1 = teacherInfoRepositiory.findById(key) ;
                TeacherInfo teacherInfo = optional1.get() ;
                System.out.println(teacherInfo);
                msgList.setIcon(teacherInfo.getIcon());
                msgList.setName(teacherInfo.getUserName());
            }
            msgLists.add(msgList) ;
//            System.out.println(key);
//            System.out.println(value);
        });
        return msgLists ;
    }

    @Override
    public void changeMessages(String receiverId, String senderId) {
        List<Message> messages = messageRepostiory.findMessagesByReceiverIdAndSenderIdOrderByDate(receiverId, senderId) ;
        int i = 0 ;
        for (i = 0; i < messages.size(); i ++ ) {
            Message message = messages.get(i) ;
            message.setType(1);
            messages.set(i, message);
            messageRepostiory.save(message) ;
        }
    }

    @Override
    public List<Message> findMessagesByReceiverIdAndSenderIdAndType(String receiverId, String senderId, long type) {
        List<Message> messages = messageRepostiory.findMessagesByReceiverIdAndSenderIdOrderByDate(receiverId, senderId);
        List<Message> messageList = messageRepostiory.findMessagesByReceiverIdAndSenderIdOrderByDate(senderId, receiverId);
        messages.addAll(messageList);
        return messages;
    }


}
